#include "Ardunio.h"
#include <plib.h>              //PIC32 Description and Libraries

void 
delay(unsigned t)
{
	//uses internal timer 1
	T1CON = 0x8000;
		while (t--) {
		TMR1 = 0;
		//while (TMR1 < 8000);
		while (TMR1 < REALCLK / COUNT1MS);

	}
}


/*
Configures the specified pin to behave either as an input or an output.
*/



void 
pinMode(int pin, int mode)
{
	//pin determination ** this is wired to the PIC32MX250F128B 28 pin part
	switch (pin) {
	case 2:
		//RA0
		if (mode == 0)
			mPORTASetPinsDigitalOut(BIT_0);
		else
			mPORTASetPinsDigitalIn(BIT_0);
		return;
	case 3:
		//RA1
			if (mode == 0)
			mPORTASetPinsDigitalOut(BIT_1);
		else
			mPORTASetPinsDigitalIn(BIT_1);
		return;
	case 4:
		//RB0
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_0);
		else
			mPORTBSetPinsDigitalIn(BIT_0);
		return;
	case 5:
		//RB1
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_1);
		else
			mPORTBSetPinsDigitalIn(BIT_1);
		return;
	case 6:
		//RB2
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_2);
		else
			mPORTBSetPinsDigitalIn(BIT_2);
		return;
	case 7:
		//RB3
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_3);
		else
			mPORTBSetPinsDigitalIn(BIT_3);
		return;
	case 9:
		//RA2
			if (mode == 0)
			mPORTASetPinsDigitalOut(BIT_2);
		else
			mPORTASetPinsDigitalIn(BIT_2);
		return;
	case 10:
		//RA3
			if (mode == 0)
			mPORTASetPinsDigitalOut(BIT_3);
		else
			mPORTASetPinsDigitalIn(BIT_3);
		return;
	case 11:
		//RB4
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_4);
		else
			mPORTBSetPinsDigitalIn(BIT_4);
		return;
	case 12:
		//RA4
			if (mode == 0)
			mPORTASetPinsDigitalOut(BIT_4);
		else
			mPORTASetPinsDigitalIn(BIT_4);
		return;
	case 14:
		//RB5
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_5);
		else
			mPORTBSetPinsDigitalIn(BIT_5);
		return;
	case 16:
		//RB7
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_7);
		else
			mPORTBSetPinsDigitalIn(BIT_7);
		return;
	case 17:
		//RB8
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_8);
		else
			mPORTBSetPinsDigitalIn(BIT_8);
		return;
	case 18:
		//RB9
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_9);
		else
			mPORTBSetPinsDigitalIn(BIT_9);
		return;
	case 21:
		//RB10
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_10);
		else
			mPORTBSetPinsDigitalIn(BIT_10);
		return;
	case 22:
		//RB11
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_11);
		else
			mPORTBSetPinsDigitalIn(BIT_11);
		return;
	case 24:
		//RB13
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_13);
		else
			mPORTBSetPinsDigitalIn(BIT_13);
		return;
	case 25:
		//RB14
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_14);
		else
			mPORTBSetPinsDigitalIn(BIT_14);
		return;
	case 26:
		//RB15
			if (mode == 0)
			mPORTBSetPinsDigitalOut(BIT_15);
		else
			mPORTBSetPinsDigitalIn(BIT_15);
		return;
	default:
		//pin 1 MCLR, pins 8, 19, 27 Vss
			// pin 13, 28 Vdd, pin 20 Vcap, pin 15 VBUS
			// pin 23 VUSB and any pin less 1 or greater then 28
			return;


	}
}

/*
 * Sets a pin configured as OUTPUT to either a HIGH or a LOW state at the
 * specified pin. The digitalWrite() function is also used to set pullup
 * resistors when a pin is configured as an INPUT.
 */
void 
digitalWrite(int pin, int value)
{
	//pin determination ** this is wired to the PIC32MX250F128B 28 pin part
	switch (pin) {
	case 2:
		//RA0
		if (value == 0)
			mPORTAClearBits(BIT_0);
		else
			mPORTASetBits(BIT_0);
		return;
	case 3:
		//RA1
			if (value == 0)
			mPORTAClearBits(BIT_1);
		else
			mPORTASetBits(BIT_1);
		return;

	case 4:
		//RB0
			if (value == 0)
			mPORTBClearBits(BIT_0);
		else
			mPORTBSetBits(BIT_0);
		return;
	case 5:
		//RB1
			if (value == 0)
			mPORTBClearBits(BIT_1);
		else
			mPORTBSetBits(BIT_1);
		return;
	case 6:
		//RB2
			if (value == 0)
			mPORTBClearBits(BIT_2);
		else
			mPORTBSetBits(BIT_2);
		return;
	case 7:
		//RB3
			if (value == 0)
			mPORTBClearBits(BIT_3);
		else
			mPORTBSetBits(BIT_3);
		return;
	case 9:
		//RA2
			if (value == 0)
			mPORTAClearBits(BIT_2);
		else
			mPORTASetBits(BIT_2);
		return;
	case 10:
		//RA3
			if (value == 0)
			mPORTAClearBits(BIT_3);
		else
			mPORTASetBits(BIT_3);
		return;
	case 11:
		//RB4
			if (value == 0)
			mPORTBClearBits(BIT_4);
		else
			mPORTBSetBits(BIT_4);
		return;
	case 12:
		//RA4
			if (value == 0)
			mPORTAClearBits(BIT_4);
		else
			mPORTASetBits(BIT_4);
		return;
	case 14:
		//RB5
			if (value == 0)
			mPORTBClearBits(BIT_5);
		else
			mPORTBSetBits(BIT_5);
		return;
	case 16:
		//RB7
			if (value == 0)
			mPORTBClearBits(BIT_7);
		else
			mPORTBSetBits(BIT_7);
		return;
	case 17:
		//RB8
			if (value == 0)
			mPORTBClearBits(BIT_8);
		else
			mPORTBSetBits(BIT_8);
		return;
	case 18:
		//RB9
			if (value == 0)
			mPORTBClearBits(BIT_9);
		else
			mPORTBSetBits(BIT_9);
		return;
	case 21:
		//RB10
			if (value == 0)
			mPORTBClearBits(BIT_10);
		else
			mPORTBSetBits(BIT_10);
		return;
	case 22:
		//RB11
			if (value == 0)
			mPORTBClearBits(BIT_11);
		else
			mPORTBSetBits(BIT_11);
		return;
	case 24:
		//RB13
			if (value == 0)
			mPORTBClearBits(BIT_13);
		else
			mPORTBSetBits(BIT_13);
		return;
	case 25:
		//RB14
			if (value == 0)
			mPORTBClearBits(BIT_14);
		else
			mPORTBSetBits(BIT_14);
		return;
	case 26:
		//RB15
			if (value == 0)
			mPORTBClearBits(BIT_15);
		else
			mPORTBSetBits(BIT_15);
		return;
	default:
		//pin 1 MCLR, pins 8, 19, 27 Vss
			// pin 13, 28 Vdd, pin 20 Vcap, pin 15 VBUS
			// pin 23 VUSB and any pin less 1 or greater then 28
			return;
	}
}
